package com.andyou.modules.sys.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.andyou.common.config.Global;
import com.andyou.common.mapper.JsonMapper;
import com.andyou.common.persistence.Page;
import com.andyou.common.utils.DateUtils;
import com.andyou.common.utils.GridVO;
import com.andyou.common.utils.IdGen;
import com.andyou.common.utils.StringUtils;
import com.andyou.common.utils.excel.ExportExcel;
import com.andyou.common.web.BaseController;
import com.andyou.rest.entity.AppInfo;
import com.andyou.rest.service.AppInfoService;
import com.google.common.collect.Lists;


/**
 * 接口接入管理Controller
 * @author lgx
 * @version 2017-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/appInfo")
public class AppInfoController extends BaseController {

	@Autowired
	private AppInfoService appInfoService;
	
	@ModelAttribute
	public AppInfo get(@RequestParam(required=false) String id) {
		AppInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = appInfoService.get(id);
		}
		if (entity == null){
			entity = new AppInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:appInfo:view")
	@RequestMapping(value = {"appInfoList", ""})
	public String list(AppInfo appInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/appInfoList";
	}
	
	@RequiresPermissions("sys:appInfo:view")
	@ResponseBody
	@RequestMapping(value = { "appInfoListJson" })
	public String appInfoListJson(AppInfo appInfo, HttpServletRequest request,
			HttpServletResponse response, Model model) {
	
		Page<AppInfo> page = appInfoService.findPage(new Page<AppInfo>(request, response,true),
				appInfo);
		model.addAttribute("page", page);

		GridVO gridVO = new GridVO();
		gridVO.setRows(page.getList());
		gridVO.setTotal(page.getCount());
		
		return JsonMapper.getInstance().toJson(gridVO);
	}

	@RequiresPermissions("sys:appInfo:view")
	@RequestMapping(value = "appInfoForm")
	public String form(AppInfo appInfo, Model model) {
		if (StringUtils.isBlank(appInfo.getId())) {
			appInfo.setAppKey(IdGen.uuid());
		}
		
		model.addAttribute("appInfo", appInfo);
		return "modules/sys/appInfoForm";
	}
	
	@RequiresPermissions("sys:appInfo:view")
	@RequestMapping(value = "appInfoDetail")
	public String appInfoDetail(AppInfo appInfo, Model model){
		model.addAttribute("appInfo", appInfo);
		return "modules/sys/appInfoDetail";
	}

	@RequiresPermissions("sys:appInfo:edit")
	@RequestMapping(value = "save")
	public String save(AppInfo appInfo, Model model, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, appInfo)){
			return form(appInfo, model);
		}
		appInfoService.save(appInfo);
		renderStringByJbox(response, "提交成功");
		return null;
		
	}
	
	@RequiresPermissions("sys:appInfo:delete")
	@RequestMapping(value = "delete")
	public String delete(AppInfo appInfo, RedirectAttributes redirectAttributes) {
		appInfoService.delete(appInfo);
		addMessage(redirectAttributes, "删除接口接入管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/appInfo/?repage";
	}
	
	@RequiresPermissions("sys:appInfo:delete")
    @ResponseBody
	@RequestMapping(value = "deleteAjax")
	public String deleteAjax(AppInfo appInfo, RedirectAttributes redirectAttributes) {
		appInfoService.delete(appInfo);
		return "删除成功";
	}
	
	@RequiresPermissions("sys:appInfo:delete")
	@ResponseBody
	@RequestMapping(value = "batchDelete") 
	public String batchDelete(String ids, RedirectAttributes redirectAttributes) {
		
        String[]  idsArr =  ids.split(",");
        List<String> idList = Lists.newArrayList();
        idList = Arrays.asList(idsArr);
        
        appInfoService.batchDelete(idList);
	
		return "删除成功";
	}
	
	@RequestMapping(value = "export")
	public String exportFile(AppInfo appInfo, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "接口接入管理" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
			Page<AppInfo> page_ = new Page<AppInfo>(request, response, -1);
			page_.setPageSize(500000);
			appInfo.setPage(page_);
			page_ = appInfoService.findPage(page_, appInfo);
			new ExportExcel("接口接入管理", AppInfo.class).setDataList(page_.getList())
					.write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出数据失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/sys/appInfo/?repage";
	}

}